# -*- coding:utf-8 -*-
# author: lkz
# date: 2016/12/20 14:17

import json

from ssig_sdk import Client

# 注: tsdb的 metric, tag, tag value 三个相同的数据可做聚合


def send_to_tsdb(ip_data, timestamp, debug=True):
    params = {
        "metric": "ping" if not debug else "ping_test",
        "data": []
    }

    for ip, data in ip_data.items():
        params["data"].extend([
            {
                "tags": {"average_rtt": ip},
                "value": data[-1],
                "timestamp": timestamp
            }
        ])

    client = Client("stream.watch.monitor.sina.com.cn", 3020)
    client.bind_auth("2016121417", "vtknIvQTEToLQEuzF6463SQkZ6WOpf")
    client.set_timeout(5)

    ret = client.post("metric", {"data": json.dumps([params])})
    ret_json = json.loads(ret)

    return ret_json["code"] == 0
